package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.TbFleetCostService;
import com.cloudlead.commonData.vo.TbFleetCost;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.fleetAction.service.FleetCostSer;
import com.cloudlead.fleetService.vo.BaseFleetCostVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Will on 2015/12/17.
 */
@Service
public class FleetCostSerImpl implements FleetCostSer {
    @Autowired
    TbFleetCostService tbFleetCostService;
    @Override
    public String addFleetCostService(BaseFleetCostVo baseFleetCostVo) {
        TbFleetCost tbFleetCost = new TbFleetCost();
        TbFleetOrder tbFleetOrder = new TbFleetOrder();
        if(null != baseFleetCostVo.getFleetOrderId() && !"".equals(baseFleetCostVo.getFleetOrderId())){
            tbFleetOrder.setId(baseFleetCostVo.getFleetOrderId());
        }
        BeanUtils.copyProperties(baseFleetCostVo,tbFleetCost);
        tbFleetCost.setBaseFleetOrder(tbFleetOrder);
        tbFleetCost.setFlag("0");
        tbFleetCostService.create(tbFleetCost);
        return "ok";
    }

    @Override
    public String delFleetCostService(String fleetCustomerId) {
       TbFleetCost tbFleetCost = tbFleetCostService.view(fleetCustomerId);
        tbFleetCost.setFlag("1");
        tbFleetCostService.update(tbFleetCost);
        return "ok";
    }

    @Override
    public String editFleetCostService(BaseFleetCostVo baseFleetCostVo) {
        TbFleetCost tbFleetCost = new TbFleetCost();
        TbFleetOrder tbFleetOrder = new TbFleetOrder();
        if(null != baseFleetCostVo.getFleetOrderId() && !"".equals(baseFleetCostVo.getFleetOrderId())){
            tbFleetOrder.setId(baseFleetCostVo.getFleetOrderId());
        }
        BeanUtils.copyProperties(baseFleetCostVo,tbFleetCost);
        tbFleetCost.setBaseFleetOrder(tbFleetOrder);
        tbFleetCost.setFlag("0");
        tbFleetCostService.update(tbFleetCost);
        return "ok";
    }

    @Override
    public Page findAll(BaseFleetCostVo baseFleetCostVo, int rows, int page) {
        PageRequest pageRequest = new PageRequest();
        List<Term> list = new ArrayList<Term>();
        list.add(new Term("flag","0"));
        if(null != baseFleetCostVo.getFleetOrderId() && !"".equals(baseFleetCostVo.getFleetOrderId())){
            list.add(new Term("baseFleetOrder.id",baseFleetCostVo.getFleetOrderId()));
        }
        Filter filter  = new Filter(new Criteria(list));
        pageRequest.setFilter(filter);
        pageRequest.setSize(rows);
        pageRequest.setPage(page);
        return tbFleetCostService.findAll(pageRequest);
    }
}
